package cn.edu.scau.cmi.zengGuanSheng.springAndhibernate.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactoryUtil {
	private static SessionFactory sf;

	static {
		sf = new Configuration().configure().buildSessionFactory();
	}

	public static SessionFactory getSessionFactory() {
		if (sf == null) {
			sf = new Configuration().configure().buildSessionFactory();
		}
		return sf;
	}

	public static Session getSession() {
		Session s = getSessionFactory().openSession();
		s.beginTransaction();
		return s;
	}

	public static void commitTransactionAndClose(Session s) {
		s.getTransaction().commit();
		s.close();
	}
}
